#include<ros/ros.h>
#include<pcl/point_cloud.h>
#include<pcl/io/pcd_io.h>
#include<pcl/io/io.h>
#include<pcl/visualization/cloud_viewer.h>
#include<pcl_conversions/pcl_conversions.h>
#include<sensor_msgs/PointCloud2.h>
#include<iostream>

using namespace std;

int main(int argc, char **argv)
{
    ros::init(argc, argv, "show");
    ros::NodeHandle nh;
    ros::Publisher pcl_pub = nh.advertise<sensor_msgs::PointCloud2> ("cloud", 1);
    // 采用XYZRGB通道来接收点云
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>);
    pcl::io::loadPCDFile("/home/czs/桌面/pcds/1.pcd",*cloud);

    sensor_msgs::PointCloud2 output;

    // 将读取的点云转化为ros消息
    pcl::toROSMsg(*cloud,output);
    output.header.frame_id = "odom";     //大概是跟坐标系有关系

    ros::Rate loop_rate(1);
    while (ros::ok())   // ctrl+C会使得ros::ok()返回false，从而结束节点运行
    {
        pcl_pub.publish(output); // 订阅了cloud话题都将接收到点云数据
        ros::spinOnce();
        loop_rate.sleep();
    }
    return 0;
    // cout<<*cloud<<endl;
    // 创建viewer对象，其实就是创建了一个窗口
    // boost::shared_ptr< pcl::visualization::PCLVisualizer > viewer(new pcl::visualization::PCLVisualizer("Ransac"));
    // pcl::visualization::PCLVisualizer  viewer;
    // viewer.setBackgroundColor(0, 0, 0);
    // int vp;
	// viewer->createViewPort(0.0, 0.0, 1.0, 1.0, vp);

    // viewer.addPointCloud<pcl::PointXYZRGB>(cloud,"source");
    // viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "source");
    // viewer.spin();
}